package com.efast.dmom.equipment.opc.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.efast.cafe.commponent.job.base.AbstractStatefulJobBean;
import com.efast.cafe.commponent.job.bean.ScheduleExecuteLog;
import com.efast.cafe.commponent.log.ext.CafeLogger;
import com.efast.cafe.commponent.log.ext.CafeLoggerFactory;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.equipment.opc.client.EquipMonitorService;
import com.efast.dmom.equipment.opc.client.OPCConnector;

public class EquipStatusMonitor extends AbstractStatefulJobBean {

	@Override
	protected void executeInternal(JobExecutionContext context, ScheduleExecuteLog cachelog)
			throws JobExecutionException {

		CafeLogger log = CafeLoggerFactory.getLogger(EquipStatusMonitor.class);
		EquipMonitorService monitorService = SpringContextUtils.getBean(EquipMonitorService.class);

		log.info("Start Equipment Status Monitor");
		try {

			boolean isConnected = OPCConnector.getInstance().checkConnection();

			if (!isConnected) {
				monitorService.initConnection();
			}
			monitorService.doBroadcast();

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw new JobExecutionException();
		}

		log.info("Finish Equipment Status Monitor");
	}

}
